package com.juguo.charginganimation.View.HomeFragment;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.fragment.app.FragmentTransaction;
import androidx.viewpager2.widget.ViewPager2;

import com.juguo.charginganimation.Base.BaseFragment;
import com.juguo.charginganimation.R;
import com.juguo.charginganimation.View.gallery.McoolGallery;
import com.juguo.charginganimation.databinding.McoolFragmentBinding;
import com.juguo.charginganimation.utils.MyViewPager;
import com.juguo.charginganimation.View.gallery.MenuanimeGallery;
import com.juguo.charginganimation.Viewmodel.FeaturedViewModel;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.Objects;

public class MenuanimeFragment extends BaseFragment {
    private String TAG = "MenuanimeFragment";
    private FeaturedViewModel mViewModel;
    private Context mContext;
    //图片瀑布流
    private MyViewPager viewPager;
    private DrawerLayout drawerLayout;
    private ViewPager2 viewPager2;

    public static McoolFragment newInstance() {
        return new McoolFragment();
    }

    @Override
    public void onAttach(@NonNull @NotNull Context context) {
        super.onAttach(context);
        mContext = context;
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_body, container, false);
        viewPager = root.findViewById(R.id.viewpage_mcool);
        viewPager2 = getActivity().findViewById(R.id.vp2);

        return root;
    }

    @Override
    protected int initLayout() {
        return R.layout.fragment_body;
    }

    @Override
    protected void initView(View view) {

    }

    @Override
    protected void initData(Context mContext) {

    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initViewPage();
    }

    /**
     * viewPage初始化
     */
    private void initViewPage() {

        //fragment数据源
        final ArrayList<Fragment> fragmentList = new ArrayList<>();
        fragmentList.add(new MenuanimeGallery());
        viewPager.setAdapter(new FragmentStatePagerAdapter(getChildFragmentManager()) {
            @NonNull
            @Override
            public Fragment getItem(int position) {
                return fragmentList.get(position);
            }

            @Override
            public int getCount() {
                return fragmentList.size();
            }
        });
        //预加载界面
        viewPager.setOffscreenPageLimit(fragmentList.size() - 1);
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    @SuppressLint("UseRequireInsteadOfGet")
    @Override
    public void onResume() {
        super.onResume();
        //Touch模式下不支持焦点，也能够获得焦点使用的
        Objects.requireNonNull(getView()).setFocusableInTouchMode(true);
        //获取当前view焦点
        getView().requestFocus();
        getView().setOnKeyListener((v, keyCode, event) -> {
            //判断用户点击了手机自带的返回键
            if (event.getAction() == KeyEvent.ACTION_UP &&
                    keyCode == KeyEvent.KEYCODE_BACK) {
                FragmentManager fm = getActivity().getSupportFragmentManager();
                FragmentTransaction beginTransaction = fm.beginTransaction();
                beginTransaction.replace(R.id.rly_huanfu, new FeaturedFragment());
                beginTransaction.commit();
                return true;

            }
            return false;
        });
    }


}